﻿using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;

namespace ContractManagementSystem.BusinessLogicLayer
{
    public static class CommonLogic
    { 
        public static string ConvertToString(object value)
        {
            return value == null ? "" : value.ToString();
        }

        public static string ConvertToLower(string value)
        {
            return value != null ? value.Trim().ToLower(CultureInfo.CurrentCulture) : ""; 
        }

        public static int ConvertToNumber(string value)
        {
            if (ConvertToLower(value) == "true")
            {
                return 1;
            }
            if (ConvertToLower(value) == "false")
            {
                return 0;
            }

            if (value == null) return 0;
            
            try
            {
                return int.Parse(value, NumberStyles.Integer, CultureInfo.CurrentCulture);
            }
            catch (FormatException)
            {
                return 0;
            }
        }
 
        public static DateTime ConvertToDateTime(string value)
        {
            try
            {
                return DateTime.Parse(value, CultureInfo.CurrentCulture);
            }
            catch (FormatException)
            {
                return DateTime.Parse("01/01/1900", CultureInfo.CurrentCulture);
            }
        }

        public static string ParseQueryVariable(string queryName, HttpRequest httpRequest)
        {
            var value = httpRequest.QueryString[queryName];

            return (value != null ? HttpUtility.HtmlDecode(value) : "");
        }
        
        public static string MaskPassword(string value)
        {
            return new string('*', value.Length);
        }

        // Create an auto string from argument an original value
        public static string CreateAutoString(string originalValue)
        {
            var unsignString = ConvertToUnsignedVietnamese(originalValue.Trim());

            var items = unsignString.Split(' ');

            var lastWord = ""; // Example: Mary is a last word in string Ham mary
            var lastWordIndex = 0;

            // Get last word from string
            for (var i = items.Length - 1; i >= 0; i--)
            {
                if (items[i] == null || items[i].Trim().Length <= 0) continue;

                lastWord = items[i];
                lastWordIndex = (i - 1);
                break;
            }

            var tempCharacters = "";

            for (var i = 0; i <= lastWordIndex; i++)
            {
                if (items[i] != null && items[i].Trim().Length > 0)
                {
                    tempCharacters += items[i].Substring(0, 1);
                }
            }

            return lastWord + tempCharacters;
        }

        // Convert vietnamese with signed-characters to one with unsigned-characters
        public static string ConvertToUnsignedVietnamese(string originalValue)
        {
            var normalization = originalValue.Normalize(NormalizationForm.FormD);
            var sb = new StringBuilder();
            foreach (var item in
                from item in normalization
                let uc = CharUnicodeInfo.GetUnicodeCategory(item)
                where uc != UnicodeCategory.NonSpacingMark
                select item)
            {
                sb.Append(item);
            }
            sb = sb.Replace('Đ', 'D');
            sb = sb.Replace('đ', 'd');
            return (sb.ToString().Normalize(NormalizationForm.FormD));
        }
    }
}
